# -*- coding: utf-8 -*-
"""
@author: cxyfreedom
@desc:
https://stackoverflow.com/questions/59195915/generate-the-initial-game-board-of-a-candy-crush-like-game
"""
from pprint import pprint
from typing import List
from random import shuffle


def check_board(board):
    def check_list(lst):
        # 判断是否连续三个数一样
        return any(lst[i] == lst[i + 1] and lst[i] == lst[i + 2] for i in range(len(lst) - 2))

    for row in board:
        if check_list(row):
            return False
    for i in range(len(board[0])):
        col = [row[i] for row in board]
        if check_list(col):
            return False
    return True


# def init_board() -> List[List[int]]:
def init_board():
    board = [[]]
    cnt = 0
    # while check_board(board):
    while not check_board(board):
        board = [1, 2, 3, 4] * 9
        shuffle(board)  # 乱序
        board = [board[i:i + 6] for i in range(0, len(board), 6)]
        cnt += 1

    print(cnt)
    return board


if __name__ == '__main__':
    pprint(init_board())
